package dk.dtu.compute.course02312.matador;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.ViewGroup.LayoutParams;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity {
	public static final int FIELDWIDTH = 65;
	public static final int FIELDHEIGHT = 65;

	public static int counter;

	private GridLayout base;
	private Bitmap[] diceIcons = new Bitmap[6];
	private RelativeLayout[][] bgLayouts = new RelativeLayout[11][11];

	// private LinearLayout[][][] borders = new LinearLayout[11][11][2];

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		makeDice();
		makeBase();
		makeBackGroundPanels();

		ImageView dieView = new ImageView(this);
		dieView.setImageBitmap(this.diceIcons[2]);
		dieView.setLayoutParams(new LayoutParams(FIELDWIDTH, FIELDWIDTH));
		setContentView(this.base);
		this.createDashedBorder(this.bgLayouts[2][2], 3, 10, getResources()
				.getColor(R.color.testcolor2), Color.BLACK, Color.RED);
	}

	/**
	 * Makes the base
	 */
	private void makeBase() {
		this.base = new GridLayout(this);
		// this.factory.setSize(this.base, 11 * Field.FIELDWIDTH, 11 *
		// Field.FIELDWIDTH);
		this.base.setColumnCount(11);
		this.base.setRowCount(11);
		this.base
				.setBackgroundColor(getResources().getColor(R.color.basecolor));
		// this.base.setOpaque(true);
	}

	public void createDashedBorder(RelativeLayout layout, int thickness,
			int stroke, int background, int color1, int color2) {
		thickness = 20;
		RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
				FIELDWIDTH, FIELDHEIGHT);
		LinearLayout layout2;
		LinearLayout layout3;
		final String TAG1 = "border1";
		final String TAG2 = "border2";

		layout2 = (LinearLayout) layout.findViewWithTag(TAG1);
		layout3 = (LinearLayout) layout.findViewWithTag(TAG2);

		if ((layout2 == null) || (layout3 == null)) {
			layout2 = new LinearLayout(this);
			layout3 = new LinearLayout(this);
			layout2.setTag(TAG1);
			layout3.setTag(TAG2);
			layout.addView(layout2, params);
			layout.addView(layout3, params);
		}

		layout.setBackgroundColor(background);

		ShapeDrawable border1 = new ShapeDrawable(new RectShape());
		Paint paint1 = border1.getPaint();

		paint1.setColor(color1);
		paint1.setStyle(Style.STROKE);
		paint1.setStrokeWidth(thickness);

		layout2.setBackgroundDrawable(border1);

		ShapeDrawable border2 = new ShapeDrawable(new RectShape());
		Paint paint2 = border2.getPaint();

		paint2.setColor(color2);
		paint2.setStyle(Style.STROKE);
		paint2.setStrokeWidth(thickness);
		paint2.setPathEffect(new DashPathEffect(new float[] { stroke, stroke },
				0));

		layout3.setBackgroundDrawable(border2);
	}

	/**
	 * /** Makes the background
	 */
	private void makeBackGroundPanels() {
		boolean chooseNormalColor = true;
		for (int x = 0; x < 11; x++) {
			for (int y = 0; y < 11; y++) {
				RelativeLayout layout = new RelativeLayout(this);
				if (chooseNormalColor) {
					this.createDashedBorder(layout, 3, 20, getResources()
							.getColor(R.color.testcolor2), Color.WHITE,
							Color.YELLOW);
				} else {
					layout.setBackgroundColor(getResources().getColor(
							R.color.testcolor3));
				}
				chooseNormalColor = !chooseNormalColor;
				this.bgLayouts[x][y] = layout;
				TextView tv = new TextView(this);
				tv.setText("(" + x * 9999 + "," + y * 9999 + ")");
				tv.setHeight(FIELDWIDTH);
				tv.setWidth(FIELDHEIGHT);
				tv.setGravity(Gravity.CENTER);
				layout.addView(tv);
				this.base.addView(layout);
			}
		}
		LinearLayout layout = new LinearLayout(this);
		layout.setBackgroundColor(getResources().getColor(R.color.testcolor1));
		TextView tv = new TextView(this);
		tv.setText("(4-6,4-6)");
		tv.setHeight(FIELDWIDTH * 3);
		tv.setWidth(FIELDHEIGHT * 3);
		tv.setGravity(Gravity.CENTER);
		layout.addView(tv);
		GridLayout.LayoutParams params = new GridLayout.LayoutParams(
				GridLayout.spec(4, 3), GridLayout.spec(4, 3));
		this.base.addView(layout, params);

		LinearLayout layout2 = new LinearLayout(this);
		layout2.setBackgroundColor(getResources().getColor(R.color.testcolor1));
		TextView tv2 = new TextView(this);
		tv2.setText("(5,6)");
		tv2.setHeight(FIELDWIDTH);
		tv2.setWidth(FIELDHEIGHT);
		tv2.setGravity(Gravity.CENTER);
		layout2.addView(tv2);
		GridLayout.LayoutParams params2 = new GridLayout.LayoutParams(
				GridLayout.spec(5, 1), GridLayout.spec(6, 1));
		this.base.addView(layout2, params2);

		LinearLayout layout3 = new LinearLayout(this);
		// layout3.setBackgroundColor(getResources().getColor(R.color.testcolor1));
		TextView tv3 = new TextView(this);
		tv3.setText("(5,5)");
		tv3.setHeight(FIELDWIDTH);
		tv3.setWidth(FIELDHEIGHT);
		tv3.setGravity(Gravity.CENTER);
		layout3.addView(tv3);
		GridLayout.LayoutParams params3 = new GridLayout.LayoutParams(
				GridLayout.spec(5, 1), GridLayout.spec(5, 1));
		this.base.addView(layout3, params3);
	}

	/**
	 * Makes images of the dice
	 */
	private void makeDice() {
		Bitmap image = BitmapFactory.decodeResource(getResources(),
				R.drawable.dice);
		for (int value = 0; value < 6; value++) {
			int x = 0;
			int y = 55 * value;
			this.diceIcons[value] = Bitmap.createBitmap(image, x, y, 54, 54);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
